import DeskTop from "@/pages/DeskTop";
import UserCenter from "@/pages/UserCenter";
import Pos from "@/pages/Pos";
import Home from '@/views/Home.vue'
import NoPage from '@/views/404'
import Store from '@/store'
import router from "@/router/index"
const _import = require('@/router/_import_' + process.env.NODE_ENV)//获取组件的方法

//免授权路由配置
export const routes = [
    {
        path: '/',
        redirect: '/home' //路由重定向
    },
    {
        path: '/home',
        component: Home,
        redirect: '/desktop',
        children: [
            {
                path: '/desktop',
                component: DeskTop,
                name: '我的桌面'
            },
            {
                path: '/usercenter',
                component: UserCenter,
                name: '个人设置'
            },
            //动态添加业务功能路由...
        ]
    },
    {
        path: '/Pos',
        component: Pos,
        name: 'Pos'
    },
    {
        path: '/404',
        component: NoPage,
        name: '404'
    },
    {
        path: '*',
        redirect: { path: '/404' }
    }
]
/**  
* 格式化远程路由
* @param: asyncRouterMap [Array] 远程路由列表(菜单数据列表)  
* */
export function filterAsyncRouter(asyncRouterMap) {
    let accessedRouters = [];
    //注意这里的 asyncRouterMap 是菜单数据数组
    asyncRouterMap.forEach(menu => {
        //默认路由名称为菜单数据名称，路径为"-"，元数据为空，权限按钮默认空数组，组件默认（此为一级且有子项的菜单）
        let route = {
            name : menu.B_FullName,
            path : "-",
            meta : { btns: [], keepAlive:true },
            component : {render: (e) => e("router-view")}
        }
        //如果菜单数据有URL地址，说明该菜单有权限按钮、对应组件
        if (menu.B_UrlAddress) {
            //路径为菜单数据路径
            route.path = menu.B_UrlAddress;
            //权限按钮为菜单数据对应的按钮数据(关联字段B_ModuleId)
            Store.state.user.buttonlist.forEach(element => {
                if (element.B_ModuleId === menu.B_ModuleId)
                    route.meta.btns.push(element.B_EnCode);
            });
            try {
                route.component = _import(route.path)
            } catch (e) {
                try {
                    route.component = () => import('@/views' + route.path + '.vue');
                } catch (error) {
                    console.info('%c 当前路由 ' + route.path + '.vue 不存在，因此如法导入组件，请检查接口数据和组件是否匹配，并重新登录，清空缓存!', "color:red")
                }
            }
        }
        //如果菜单数据有子项，递归
        if (menu.children && menu.children.length > 0) {
            route.children = filterAsyncRouter(menu.children)
        } 
        accessedRouters.push(route);
    });
    return accessedRouters;
}
/**  
* 合并远程路由到本地路由   
* @param: asyncRoutes [Array] 远程路由列表(菜单数据列表)  
* @param: routes [Array] 本地路由列表  
* */
export function mergeRoutes(asyncRoutes) {
    asyncRoutes.forEach(element => {
        routes[1].children.push(element)
    });
    // 防止重复配置相同路由
    if (router.options.routes.length <= 1) {
        router.addRoutes(routes);
        // this.$router不是响应式的，所以手动将路由元注入路由对象
        router.options.routes.push(routes);
    }
}

